import asyncio
import time, re
from urllib.parse import urljoin

import aiohttp
from aiomysql import create_pool
from pyquery import PyQuery as pq

base_url = "http://192.168.10.12/5/"
start_urls = ["http://192.168.10.12/5/mvlist_tag.aspx?sortID=0"]
waitting_urls = ["http://192.168.10.12/5/Default.aspx"]
seen_urls = set()
Stop = False


# 返回爬去页面
async def fetch(session, url):
    try:
        print('starting halder url:[{}]'.format(url))
        async with session.get(url) as response:
            return await response.text()
    except Exception as e:
        print(e)


async def moive_handler(session, url):
    content = await fetch(session, url)
    jq = pq(content)
    for moive in jq('div.movieListOne'):
        a = jq(moive).find('a').eq(1)
        MoiveHref = urljoin(base_url, jq(a).attr('href'))
        MoiveID = MoiveHref.split('=')[-1]
        MoivePic = urljoin(base_url, jq(a).find('img').attr('src'))
        MoiveName = jq(a).attr('title')
        MoiveActors = re.match(r'主演：(.*)', jq(moive).find('p').eq(0).text()).group(1)
        MoiveDate = re.match(r'更新：(.*)', jq(moive).find('p').eq(1).text()).group(1)[:9]
        play_text = jq(moive).find('p').eq(2).text()
        MoivePlay = 0
        try:
            MoivePlay = re.match(r'.*：(\d+)次.*', play_text, re.S).group(1)
        except:
            pass
        moive = {
            "MoiveID": MoiveID,
            "MoiveName": MoiveName,
            "MoiveHref": MoiveHref,
            "MoivePic": MoivePic,
            "MoiveActors": MoiveActors,
            "MoiveDate": MoiveDate,
            "play_text": play_text,
            "MoivePlay": MoivePlay
        }
        try:
            await toDataDb(session, moive)
            seen_urls.add(MoiveHref)
        except:
            print('the moive {} is to DB fail'.find(MoiveName))


async def toDataDb(session, moive):
    async with create_pool(host='192.168.99.100', port=3306,
                           user='root', password='root',
                           db='hdmoives', loop=loop, chartset="utf8") as pool:
        async with pool.get() as conn:
            async with conn.cursor() as cur:
                insert_sql = '''
                insert into Moives (MoiveID, MoiveName, MoiveHref, MoivePic, MoiveActors, MoiveDate, MoivePlay)
                values ('(MoiveID)%s', '(MoiveName)%s', '(MoiveHref)%s', '(MoivePic)%s', '(MoiveActors)%s', '(MoiveDate)%s', '(MoivePlay)%s')
                ''' % moive
                await cur.execute(insert_sql)


async def init_urls(session, url):
    while not Stop:
        html = await fetch(session, url)
        jq = pq(html)
        for a in jq('a'):
            href = urljoin(base_url, jq(a).attr('href'))
            if 'mvlist' in href and href not in waitting_urls: waitting_urls.append(href)


async def main():
    async with aiohttp.ClientSession() as session:
        html = await moive_handler(session, start_urls.pop())
        print(html)


if __name__ == '__main__':
    # ss='更新：2018/7/31 9:49:19'
    # print(re.match(r'更新：(.*)',ss).group(1))
    print('start')
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
